import ElementUI from 'element-ui'
import axios from 'axios';

var ajax = axios.create({
    baseURL: process.env.BASE_URL, //测试
    // baseURL: 'http://api.gongxiangdiancan.com', //正式服
    headers: {},
    withCredentials: true, //cookie
    crossDomain: true //跨域
});

//添加一个请求拦截器
ajax.interceptors.request.use(function(config) {
    //在请求发出之前进行一些操作
    if (sessionStorage.getItem('jwt')) {
        config.headers.TOKEN = sessionStorage.getItem('jwt');
    }
    return config;
}, function(err) {
    console.error(err);
    ElementUI.Message.error({
        message: '请求发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with request error
    return Promise.reject(err);
});
//添加一个响应拦截器
ajax.interceptors.response.use(function(res) {
    //在这里对返回的数据进行处理
    if (!res.data.status) {
        if(res.data.errorCode==401){
            location.href = '/login'
        }
        ElementUI.Message.error({
            message: res.data.message,
            type: 'error'
        });
        throw new Error(res.data.message);
    }

    return (res.data.data?res.data.data:res.data.status);
}, function(err) {
    console.error(err);
    ElementUI.Message.error({
        message: '响应发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with response error
    return Promise.reject(err);
});


//管理员
export const adminLogin = params => {
    return ajax.post('agent/agent/login', params);
};
export const updatePwd = params => {
    return ajax.post('agent/agent/secretkey', params);
};
export const getAdminInfo = params => {
    return ajax.get('admin/admin', params);
};
export const getAdminLogs = params => {
    return ajax.get('admin/adminLogging', params);
};

//业务员列表
export const SalesmanList = params => {
	return ajax.get('agent/merchandiser', params);
};
//业务员详情
export const getSalesmanInfo = merchandiserId => {
	return ajax.get('agent/merchandiser/' +merchandiserId);
};
//添加业务员
export const addSalesman = params => {
	return ajax.put('agent/merchandiser', params);
};
//编辑业务员
export const updateSalesman = (merchandiserId,params) => {
	return ajax.post('agent/merchandiser/' + merchandiserId, params);
};

//获取审核通过的店铺列表
export const shopList = params => {
    return ajax.get('agent/shop', params);
};
export const exportShopList = params => {
    return ajax.get('agent/shop/export', params);
};
//审核通过的店铺详情
export const getShopInfo = shopId => {
	return ajax.get('agent/shop/' +shopId);
};


//获取未审核的店铺列表
export const shopAuditList = params => {
	return ajax.get('agent/openStore', params);
};
//审核记录详情
export const getAuditLogDetail = id => {
	return ajax.get('agent/auditLogging/' + id);
};
export const exportShopAuditList = params => {
	return ajax.get('agent/openStore/export', params);
};
export const getShopAuditInfo = shopId => {
	return ajax.get('agent/openStore/' +shopId);
};
//提交资料给蜂鸟审核
export const commitToDelivery = shopId => {
	return ajax.post('agent/openStore/distribution/commit/' + shopId);
};

//开店
//获取短信验证码
export const getPhoneCode = phoneNumber => {
	return ajax.post('commons/phoneCode/' + phoneNumber);
};
//添加店铺
export const addShop = params => {
	return ajax.post('agent/openStore/addSeller', params);
};
//获取店铺分类
export const getShopCategoryList = () => {
	return ajax.get('seller/shopCategory');
};


//获取店铺信息
export const getShopBaseInfo = shopId => {
	return ajax.get('agent/openStore/base/get/' + shopId);
};
export const saveShopBaseInfo = (shopId,params) => {
	return ajax.put('agent/openStore/base/update/' + shopId, params);
};

// get shopInfo
export const findShopAuditById = (type, shopId) => {
	return ajax.get('agent/openStore/'+ type +'/get/' + shopId);
};
//pass
export const passShopAudit = (type, params) => {
	return ajax.post('agent/openStore/'+ type +'/pass', params);
};
//reject base
export const rejectShopAudit = (type, params) => {
	return ajax.post('agent/openStore/'+ type +'/unpass', params);
};


//商家资质信息
export const saveShopQualificationInfo = (shopId,params) => {
	return ajax.put('agent/openStore/qualification/update/' + shopId, params);
};
export const getShopQualificationInfo = shopId => {
	return ajax.get('agent/openStore/qualification/get/' + shopId);
};



//获取银行卡信息
export const getBankCardInfoByCardNumber = cardNumber => {
	return ajax.get('commons/bankCard/' + cardNumber);
};


//商家结算信息
export const saveShopSettleInfo = (shopId,params) => {
	return ajax.put('agent/openStore/settlement/update/' + shopId, params);
};
export const getShopSettleInfo = shopId => {
	return ajax.get('agent/openStore/settlement/get/' + shopId);
};


//店铺logo
export const setShopLogo = (shopId,params) => {
	return ajax.post('agent/openStore/logo/' + shopId, params);
};

//文件下载
export const getUploadFiles = () => {
	return axios.get('http://cdn.gongxiangdiancan.com/operation/file.json');
};


//文件上传 前台文件需要设置一个path属性
export const uploadFiles = params => {
    return ajax.post('commons/upload' + params.path, params);
};

